﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace PayloadTracker
{
    class APRSSymbols
    {
        ImageList m_ImageList = new ImageList();
        Image set1 = Properties.Resources.set1 as Image;
        Image set2 = Properties.Resources.set2 as Image;
        string special = null;
        //step is the size (in pixels) of each tile in the parent image. 
        //tiles are assumed to be square, eg. 16x16
        int step = 16;
        int disp = 20;

        public ImageList ImageList
        {
            get { return m_ImageList; }
        }

        public APRSSymbols()
        {
            initImageList();
        }

        private void initImageList()
        {
            ImageList il = new ImageList();
            for (int i = 0; i < SymbolMetaData.Length; i++)
            {
                int set = 1;
                if (i > 95)
                    set = 2;
                Image img = Tile(set, i);
                il.Images.Add(SymbolMetaData[i][0], img);
            }
            m_ImageList = il;
        }

        //metadata about symbols. index in this array corresponds to index in imageset
        public string[][] SymbolMetaData
        {
            get { return SymbolMetaData2; }
        }
        public static string[][] SymbolMetaData2 = new string[][]
        {
            new string[]{"","","Nothing"},
            new string[]{"!","BB","Police Stn"},
            new string[]{"\"","BC","\"  - (No Symbol)"},
            new string[]{"#","BD","Digi"},
            new string[]{"$","BE","Phone"},
            new string[]{"%","BF","DX Cluster"},
            new string[]{"&","BG","HF Gateway"},
            new string[]{"'","BH","Plane sm"},
            new string[]{"(","BI","Mob Sat Stn"},
            new string[]{")","BJ","WheelChair"},
            new string[]{"*","BK","Snowmobile"},
            new string[]{"+","BL","Red Cross"},
            new string[]{",","BM","Boy Scout"},
            new string[]{"-","BN","Home"},
            new string[]{".","BO","X"},
            new string[]{"/","BP","Red Dot"},
            new string[]{"0","P0","Circle (0)"},
            new string[]{"1","P1","Circle (1)"},
            new string[]{"2","P2","Circle (2)"},
            new string[]{"3","P3","Circle (3)"},
            new string[]{"4","P4","Circle (4)"},
            new string[]{"5","P5","Circle (5)"},
            new string[]{"6","P6","Circle (6)"},
            new string[]{"7","P7","Circle (7)"},
            new string[]{"8","P8","Circle (8)"},
            new string[]{"9","P9","Circle (9)"},
            new string[]{":","MR","Fire"},
            new string[]{";","MS","Campground"},
            new string[]{"<","MT","Motorcycle"},
            new string[]{"=","MU","Rail Eng."},
            new string[]{">","MV","Car"},
            new string[]{"?","MW","File svr"},
            new string[]{"@","MX","HC Future"},
            new string[]{"A","PA","Aid Stn"},
            new string[]{"B","PB","BBS"},
            new string[]{"C","PC","Canoe"},
            new string[]{"D","PD","D - (No Symbol)"},
            new string[]{"E","PE","Eyeball"},
            new string[]{"F","PF","Tractor"},
            new string[]{"G","PG","Grid Squ."},
            new string[]{"H","PH","Hotel"},
            new string[]{"I","PI","Tcp/ip"},
            new string[]{"J","PJ","J - (No Symbol)"},
            new string[]{"K","PK","School"},
            new string[]{"L","PL","Usr Log-ON"},
            new string[]{"M","PM","MacAPRS"},
            new string[]{"N","PN","NTS Stn"},
            new string[]{"O","PO","Balloon"},
            new string[]{"P","PP","Police"},
            new string[]{"Q","PQ","TBD"},
            new string[]{"R","PR","Rec Veh'le"},
            new string[]{"S","PS","Shuttle"},
            new string[]{"T","PT","SSTV"},
            new string[]{"U","PU","Bus"},
            new string[]{"V","PV","ATV"},
            new string[]{"W","PW","WX Service"},
            new string[]{"X","PX","Helo"},
            new string[]{"Y","PY","Yacht"},
            new string[]{"Z","PZ","WinAPRS"},
            new string[]{"[","HS","Jogger"},
            new string[]{"\\","HT","Triangle"},
            new string[]{"]","HU","PBBS"},
            new string[]{"^","HV","Plane lrge"},
            new string[]{"_","HW","WX Station"},
            new string[]{"`","HX","Dish Ant."},
            new string[]{"a","LA","Ambulance"},
            new string[]{"b","LB","Bike"},
            new string[]{"c","LC","ICP"},
            new string[]{"d","LD","Fire Station"},
            new string[]{"e","LE","Horse"},
            new string[]{"f","LF","Fire Truck"},
            new string[]{"g","LG","Glider"},
            new string[]{"h","LH","Hospital"},
            new string[]{"i","LI","IOTA"},
            new string[]{"j","LJ","Jeep"},
            new string[]{"k","LK","Truck"},
            new string[]{"l","LL","Laptop"},
            new string[]{"m","LM","Mic-E Rptr"},
            new string[]{"n","LN","Node"},
            new string[]{"o","LO","EOC"},
            new string[]{"p","LP","Rover"},
            new string[]{"q","LQ","Grid squ."},
            new string[]{"r","LR","Antenna"},
            new string[]{"s","LS","Power Boat"},
            new string[]{"t","LT","Truck Stop"},
            new string[]{"u","LU","Truck 18wh"},
            new string[]{"v","LV","Van"},
            new string[]{"w","LW","Water Stn"},
            new string[]{"x","LX","XAPRS"},
            new string[]{"y","LY","Yagi"},
            new string[]{"z","LZ","Shelter"},
            new string[]{"{","J1","{ - (No Symbol)"},
            new string[]{"|","J2","TNC Stream Sw"},
            new string[]{"}","J3","} - (No Symbol)"},
            new string[]{"~","J4","TNC Stream Sw"},
            new string[]{"","","Nothing"},
            new string[]{"!","OB","Emergency"},
            new string[]{"\"","OC","\" - (No Symbol)"},
            new string[]{"#","OD","Digi A-Z,0-9"},
            new string[]{"$","OE","Bank"},
            new string[]{"%","OF","% - (No Symbol)"},
            new string[]{"&","OG","Diam'd A-Z,0-9"},
            new string[]{"'","OH","Crash site"},
            new string[]{"(","OI","Cloudy"},
            new string[]{")","OJ","MEO"},
            new string[]{"*","OK","Snow"},
            new string[]{"+","OL","Church"},
            new string[]{",","OM","Girl Scout"},
            new string[]{"-","ON","Home (HF)"},
            new string[]{".","OO","UnknownPos"},
            new string[]{"/","OP","Destination"},
            new string[]{"0","A0","Circle A-Z,0-9"},
            new string[]{"1","A1","1 - (No Symbol)"},
            new string[]{"2","A2","2 - (No Symbol)"},
            new string[]{"3","A3","3 - (No Symbol)"},
            new string[]{"4","A4","4 - (No Symbol)"},
            new string[]{"5","A5","5 - (No Symbol)"},
            new string[]{"6","A6","6 - (No Symbol)"},
            new string[]{"7","A7","7 - (No Symbol)"},
            new string[]{"8","A8","8 - (No Symbol)"},
            new string[]{"9","A9","Petrol Stn"},
            new string[]{":","NR","Hail"},
            new string[]{";","NS","Park"},
            new string[]{"<","NT","Gale Fl"},
            new string[]{"=","NU","= - (No Symbol)"},
            new string[]{">","NV","Car A-Z,0-9"},
            new string[]{"?","NW","Info Kiosk"},
            new string[]{"@","NX","Hurricane"},
            new string[]{"A","AA","Box A-Z,0-9"},
            new string[]{"B","AB","Snow blwng"},
            new string[]{"C","AC","Coast G'rd"},
            new string[]{"D","AD","Drizzle"},
            new string[]{"E","AE","Smoke"},
            new string[]{"F","AF","Fr'ze Rain"},
            new string[]{"G","AG","Snow Shwr"},
            new string[]{"H","AH","Haze"},
            new string[]{"I","AI","Rain Shwr"},
            new string[]{"J","AJ","Lightning"},
            new string[]{"K","AK","Kenwood"},
            new string[]{"L","AL","Lighthouse"},
            new string[]{"M","AM","M - (No Symbol)"},
            new string[]{"N","AN","Nav Buoy"},
            new string[]{"O","AO","Rocket"},
            new string[]{"P","AP","Parking"},
            new string[]{"Q","AQ","Quake"},
            new string[]{"R","AR","Restaurant"},
            new string[]{"S","AS","Sat/Pacsat"},
            new string[]{"T","AT","T'storm"},
            new string[]{"U","AU","Sunny"},
            new string[]{"V","AV","VORTAC"},
            new string[]{"W","AW","WXS A-Z,0-9"},
            new string[]{"X","AX","Pharmacy"},
            new string[]{"Y","AY","Y - (No Symbol)"},
            new string[]{"Z","AZ","Z - (No Symbol)"},
            new string[]{"[","DS","Wall Cloud"},
            new string[]{"\\","DT","\\ - (No Symbol)"},
            new string[]{"]","DU","] - (No Symbol)"},
            new string[]{"^","DV","Plane A-Z,0-9"},
            new string[]{"_","DW","WX Stn A-Z,0-9"},
            new string[]{"`","DX","Rain"},
            new string[]{"a","SA","Diamond A-Z,0-9"},
            new string[]{"b","SB","Dust blwng"},
            new string[]{"c","SC","CivDef A-Z,0-9"},
            new string[]{"d","SD","DX Spot"},
            new string[]{"e","SE","Sleet"},
            new string[]{"f","SF","Funnel Cld"},
            new string[]{"g","SG","Gale"},
            new string[]{"h","SH","HAM store"},
            new string[]{"i","SI","Blk Box A-Z,0-9"},
            new string[]{"j","SJ","WorkZone"},
            new string[]{"k","SK","SUV"},
            new string[]{"l","SL","Area Locns"},
            new string[]{"m","SM","Milepost"},
            new string[]{"n","SN","Triang A-Z,0-9"},
            new string[]{"o","SO","Circle sm"},
            new string[]{"p","SP","Part Cloud"},
            new string[]{"q","SQ","q - (No Symbol)"},
            new string[]{"r","SR","Restrooms"},
            new string[]{"s","SS","Boat A-Z,0-9"},
            new string[]{"t","ST","Tornado"},
            new string[]{"u","SU","Truck A-Z,0-9"},
            new string[]{"v","SV","Van A-Z,0-9"},
            new string[]{"w","SW","Flooding"},
            new string[]{"x","SX","x - (No Symbol)"},
            new string[]{"y","SY","Sky Warn"},
            new string[]{"z","SZ","Shelter A-Z,0-9"},
            new string[]{"{","Q1","Fog"},
            new string[]{"|","Q2","TNC Stream SW"},
            new string[]{"}","Q3",") - (No Symbol)"},
            new string[]{"~","Q4","TNC Stream SW"}
        };

        public Bitmap tile(int index)
        {
            int set = 0;
            if (index <= 95)
                set = 1;
            else
                set = 2;
            return Tile(set, index);
        }

        public Bitmap tile(int set, int index)
        {
            return Tile(set, index);
        }

        public Bitmap tile(string setChar, int index)
        {
            int set = Imageset(setChar);
            return Tile(set, index);
        }

        public static int GetIndexofSym(char sym)
        {
            for (int i = 0; i < SymbolMetaData2.Length; i++)
                if (SymbolMetaData2[i][0] == sym.ToString())
                    return i;

            return 0;
        }

        public Bitmap tile(string setChar, string indexChar)
        {
            int index = Array.IndexOf(SymbolMetaData, indexChar);
            int set = Imageset(setChar);
            return Tile(set, index);
        }
            
        private Bitmap Tile(int set, int index)
        {
            Image image;
            if (set == 1)
                image = set1;
            else
                image = set2;

            if (index > 95)
                index = index - 95;

            Decimal dInt = Convert.ToDecimal(index / 16);
            int x = Convert.ToInt32(Math.Round(dInt, 0)) * 16;
            int y = (index % 16) * 16;
            Point SrcPt = new Point(x, y);
            Size srcSize = new Size(step, step);
            Size dstSize = new Size(disp, disp);
            Rectangle srcRec = new Rectangle(SrcPt, srcSize);
            Rectangle dstRec = new Rectangle(new Point(0, 0), dstSize);
            
            Bitmap tile = new Bitmap(disp, disp, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(tile);
            g.DrawImage(image, dstRec, srcRec, GraphicsUnit.Pixel);

            //set 2 with overlay, draw overlay
            if (special != null)
            {
                // Create font and brush.
                Rectangle fontRec = new Rectangle(new Point(-1, -2), new Size(step + 1, step + 1));
                Font shadowFont = new Font("Tahoma", 13, FontStyle.Bold);
                SolidBrush shadowBrush = new SolidBrush(Color.Black);

                // Draw string to screen.
                g.DrawString(special, shadowFont, shadowBrush, fontRec);
                Font topFont = new Font("Tahoma", 13);
                SolidBrush topBrush = new SolidBrush(Color.Yellow);

                // Draw string to tile.
                g.DrawString(special, topFont, topBrush, fontRec);
            }

            return tile;
        }

        /// <summary>
        /// returns the imageset based on the APRS set indicator character
        /// </summary>
        /// <param name="chr">String consisting of the APRS imageset charachter</param>
        /// <returns>the image set</returns>
        private int Imageset(string chr)
        {
            //set 1
            if (chr == "/")
            {
                return 1;
            }
            //set 2
            else if (chr == "\\")
            {
                return 2;
            }
            //set 2 with overlay
            else if (Regex.IsMatch(chr, @"^[0-9A-Z]{1}$"))
            {
                special = chr;
                return 2;
            }
            else
            {
                throw new Exception("Invalid character passed to APRSSymbols.Imageset");
            }
        }

        

    }
}
